package com.woniu.micropropertyserver.housemag.controller;

import com.github.pagehelper.PageInfo;
import com.woniu.microcommonsserver.entity.HouseChangeInfoEntity;
import com.woniu.microcommonsserver.entity.HouseInfoEntity;
import com.woniu.microcommonsserver.entity.OwnerInfoEntity;
import com.woniu.microcommonsserver.param.PageRequest;
import com.woniu.microcommonsserver.param.ResponseMsg;
import com.woniu.microcommonsserver.param.TableResult;
import com.woniu.micropropertyserver.bean.UnBindJson;
import com.woniu.micropropertyserver.housemag.service.impl.HouseServiceImpl;
import com.woniu.micropropertyserver.service.impl.OwnerInfoServiceImpl;
import org.apache.ibatis.annotations.Param;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/house")
public class HouseController {

    @Resource
    private HouseServiceImpl houseService;

    @Resource
    private OwnerInfoServiceImpl ownerInfoService;

    @RequestMapping(value = "/findBy",produces = {"application/json;charset=utf-8"})
    public ResponseMsg findBy(PageRequest request, HouseInfoEntity houseInfoEntity){
        Integer sNum = request.getOffset();
        Integer lNum = request.getLimit();
        Integer pNum = request.getPageNum();
        if(sNum == null || sNum < 0){
            sNum = 0;
            request.setOffset(sNum);
        }
        if(lNum == null || lNum <= 0){
            lNum = 10;
            request.setLimit(lNum);
        }
        if(pNum == null || pNum <= 0){
            pNum = 1;
            request.setPageNum(pNum);
        }
        PageInfo<HouseInfoEntity> pageInfo = houseService.findBy(request,houseInfoEntity);
        TableResult list = new TableResult<HouseInfoEntity>(pageInfo.getTotal(),pageInfo.getList());
        return ResponseMsg.builder().code(200).msg("查询成功").data(list).build();
    }

    @GetMapping(value = "/findChange",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseMsg findChange(String id){
        List<HouseChangeInfoEntity> list = houseService.findChange(id);
        return ResponseMsg.builder().code(200).msg("查询成功").data(list).build();
    }

    @GetMapping(value = "/findByCard",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseMsg findByCard(String id){
        OwnerInfoEntity o = ownerInfoService.findByCard(id);
        return ResponseMsg.builder().code(200).msg("查询成功").data(o).build();
    }


    @PostMapping(value = "/bind",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseMsg bind(@RequestBody UnBindJson unBindJson){
        houseService.bind(unBindJson.getId(),unBindJson.getUid(),unBindJson.getName(),unBindJson.getPhone());
        HouseChangeInfoEntity houseChangeInfoEntity = new HouseChangeInfoEntity();
        houseChangeInfoEntity.setFkHouseId(Long.valueOf(unBindJson.getId()));
        houseChangeInfoEntity.setDochange("业主绑定，新业主："+unBindJson.getName());
        houseChangeInfoEntity.setDoexplain("房产出售");
        houseChangeInfoEntity.setUser(unBindJson.getDoer());
        houseChangeInfoEntity.setChangetime(new Date());
        houseService.addChange(houseChangeInfoEntity);
        return ResponseMsg.builder().code(200).msg("绑定成功").data("").build();
    }

    @PostMapping(value = "/unbind",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseMsg unbind(@RequestBody UnBindJson unBindJson){
        houseService.unbind(unBindJson.getId());
        HouseChangeInfoEntity houseChangeInfoEntity = new HouseChangeInfoEntity();
        houseChangeInfoEntity.setFkHouseId(Long.valueOf(unBindJson.getId()));
        houseChangeInfoEntity.setDochange("业主取绑，原业主："+unBindJson.getName());
        houseChangeInfoEntity.setUser(unBindJson.getDoer());
        houseChangeInfoEntity.setChangetime(new Date());
        houseService.addChange(houseChangeInfoEntity);
        return ResponseMsg.builder().code(200).msg("取绑成功").data("").build();
    }

    @PostMapping(value = "/cBind",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseMsg cBind(@RequestBody UnBindJson unBindJson){
        houseService.cBind(unBindJson.getId(),unBindJson.getUid(),unBindJson.getName(),unBindJson.getPhone());
        HouseChangeInfoEntity houseChangeInfoEntity = new HouseChangeInfoEntity();
        houseChangeInfoEntity.setFkHouseId(Long.valueOf(unBindJson.getId()));
        houseChangeInfoEntity.setDochange("变更业主，新业主："+unBindJson.getName());
        houseChangeInfoEntity.setUser(unBindJson.getDoer());
        houseChangeInfoEntity.setChangetime(new Date());
        houseService.addChange(houseChangeInfoEntity);
        return ResponseMsg.builder().code(200).msg("变绑成功").data("").build();
    }

    @GetMapping(value = "/findHByCard",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseMsg findByHCard(String cardId){
       List<HouseInfoEntity>  houses = houseService.findByCard(cardId);
        return ResponseMsg.builder().code(200).msg("查询成功").data(houses).build();
    }


}
